package com.localjoycommunity.cpdj.project.system.service.impl;

import java.util.List;

import com.localjoycommunity.cpdj.common.utils.DateUtils;
import com.localjoycommunity.cpdj.project.system.domain.User;
import com.localjoycommunity.cpdj.project.system.domain.vo.UserCourseWatchVo;
import com.localjoycommunity.cpdj.project.system.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.localjoycommunity.cpdj.project.system.mapper.UserCourseWatchMapper;
import com.localjoycommunity.cpdj.project.system.domain.UserCourseWatch;
import com.localjoycommunity.cpdj.project.system.service.IUserCourseWatchService;

/**
 * Service业务层处理
 *
 * @author leepandar
 * @date 2020-12-16
 */
@Service
public class UserCourseWatchServiceImpl implements IUserCourseWatchService {
    @Autowired
    private UserCourseWatchMapper userCourseWatchMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 查询
     *
     * @param id ID
     * @return
     */
    @Override
    public UserCourseWatch selectUserCourseWatchById(Long id) {
        return userCourseWatchMapper.selectUserCourseWatchById(id);
    }

    /**
     * 用户学习历史
     *
     * @param userCourseWatchVo
     * @return
     */
    @Override
    public List<UserCourseWatchVo> selectUserCourseWatchListHistory(UserCourseWatchVo userCourseWatchVo) {
        String userName = userCourseWatchVo.getUserName();
        if (!"".equals(userName) && userName != null) {
            User user = new User();
            user.setName(userName);
            List<User> userList = userMapper.selectUserList(user);
            if (userList != null && userList.size() > 0) {
                userCourseWatchVo.setUserId(userList.get(0).getId());
            }
        }
        List<UserCourseWatchVo> list = userCourseWatchMapper.selectUserCourseWatchListHistory(userCourseWatchVo);
        for (UserCourseWatchVo courseWatchVo : list) {
            int userId = courseWatchVo.getUserId();
            User user = userMapper.selectUserById(userId);
            String name = user.getName();
            courseWatchVo.setUserName(name);
            int allWatchTime = courseWatchVo.getAllWatchTime();
            int h = allWatchTime / 3600;
            int m = (allWatchTime % 3600) / 60;
            int s = (allWatchTime % 3600) % 60;
            courseWatchVo.setAllStudyTime(h + "小时:" + m + "分钟:" + s + "秒");
            UserCourseWatch userCourseWatch = userCourseWatchMapper.selectByUpdateTimeDesc(courseWatchVo);
            if (userCourseWatch != null) {
                courseWatchVo.setUpdateTime(userCourseWatch.getUpdateTime());
            }
        }
        return list;
    }

    /**
     * 用户学习记录
     *
     * @param userCourseWatch
     * @return
     */
    @Override
    public List<UserCourseWatch> selectUserCourseWatchList(UserCourseWatch userCourseWatch) {
        List<UserCourseWatch> list = userCourseWatchMapper.selectUserCourseWatchList(userCourseWatch);
        for (UserCourseWatch courseWatch : list) {
            User user = userMapper.selectUserById(courseWatch.getUserId());
            if (user != null) {
                courseWatch.setUserName(user.getName());
            }
        }
        return list;
    }

    /**
     * 新增
     *
     * @param userCourseWatch
     * @return 结果
     */
    @Override
    public int insertUserCourseWatch(UserCourseWatch userCourseWatch) {
        userCourseWatch.setCreateTime(DateUtils.getNowDate());
        return userCourseWatchMapper.insertUserCourseWatch(userCourseWatch);
    }

    /**
     * 修改
     *
     * @param userCourseWatch
     * @return 结果
     */
    @Override
    public int updateUserCourseWatch(UserCourseWatch userCourseWatch) {
        userCourseWatch.setUpdateTime(DateUtils.getNowDate());
        return userCourseWatchMapper.updateUserCourseWatch(userCourseWatch);
    }

    /**
     * 批量删除
     *
     * @param ids 需要删除的ID
     * @return 结果
     */
    @Override
    public int deleteUserCourseWatchByIds(Long[] ids) {
        return userCourseWatchMapper.deleteUserCourseWatchByIds(ids);
    }

    /**
     * 删除信息
     *
     * @param id ID
     * @return 结果
     */
    @Override
    public int deleteUserCourseWatchById(Long id) {
        return userCourseWatchMapper.deleteUserCourseWatchById(id);
    }
}
